


// 全局定义
#define SQL_PREPARE_DEFAULT		SQLITE_PREPARE_PERSISTENT | SQLITE_PREPARE_DONT_LOG



// 补充 API 定义
uint64 GetTickCount64();



// 全局路径
str ExePath;
str AppPath;
str WebPath;
str DBPath;
str TempPath;
str PagePath;
str ToolPath;
str OptionPath;
str InstallPath;
str TemplatePath;

// HTML 载荷类型
str HTTP_CT_HTML = "Content-Type: text/html\r\n";
str HTTP_CT_TEXT = "Content-Type: text/plain\r\n";
str HTTP_CT_JSON = "Content-Type: application/json\r\n";



// 是否已安装
bool G_Install = FALSE;



// 全局 Session 表
xvalue G_Session = NULL;



// 全局配置表
xvalue G_Option = NULL;



// 全局数据库对象
XDO_Connect G_DB = NULL;



// 全局静态路由表 - HTTP
typedef struct {
	
	// 对应 URI 的处理函数
	void (*Proc)(XS_ServerObject objServer, XS_HostObject objHost, struct mg_connection* c, struct mg_http_message* hm);
	
	// 是否记录访问日志
	bool bPutLog;
	
	// 是否必须鉴权才能访问
	bool bAuth;
	
	// 所属权限组ID
	uint32 AuthID;
	
} RouteInfo;
xdict G_StaticRouteTableHTTP;

// 添加全局静态路由表项 - HTTP
void AddStaticRouteHTTP(str uri, void* proc, bool bPutLog, bool bAuth)
{
	RouteInfo* pInfo = xrtDictSet(G_StaticRouteTableHTTP, uri, strlen(uri), NULL);
	if ( pInfo ) {
		pInfo->Proc = proc;
		pInfo->bPutLog = bPutLog;
		pInfo->bAuth = bAuth;
		pInfo->AuthID = 0;
	} else {
		printf("add static http route failed : %s.\n", uri);
	}
}



// 全局权限表
xvalue G_CACHE_Role = NULL;
xvalue G_CACHE_Auth_ComboList = NULL;
xvalue G_CACHE_AuthGroup_ComboList = NULL;
xvalue G_CACHE_Role_ComboList = NULL;



// 初始化全局定义
void Define_Init(XS_ServerObject objServer, XS_HostObject objHost)
{
	ExePath = xCore->AppPath;
	WebPath = objHost->Path;
	AppPath = xrtPathGetDir(WebPath, 0);
	DBPath = xrtPathJoin(3, AppPath, "data", "db");
	TempPath = xrtPathJoin(3, AppPath, "data", "temp");
	PagePath = xrtPathJoin(3, AppPath, "data", "page");
	ToolPath = xrtPathJoin(2, ExePath, "tools");
	OptionPath = xrtPathJoin(3, AppPath, "data", "options");
	InstallPath = xrtPathJoin(3, AppPath, "data", "install");
	TemplatePath = xrtPathJoin(3, AppPath, "data", "template");
	
	// 自动创建目录
	xrtDirCreate(TempPath);
}



// 卸载全局数据
void Define_Unit()
{
	xrtFree(AppPath);
	xrtFree(DBPath);
	xrtFree(TempPath);
	xrtFree(PagePath);
	xrtFree(ToolPath);
	xrtFree(OptionPath);
	xrtFree(InstallPath);
	xrtFree(TemplatePath);
}


